package com.appspot.thinkhea;

import com.appspot.thinkhea.model.MyDBHelper;
import com.appspot.thinkhea.model.Picture;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class PanelAddPlayer extends Activity {
	protected ImageView _image;
	protected EditText _name;
	protected EditText _detail;
	protected static final String PHOTO_TAKEN = "photo_taken";
	protected boolean _taken = false;
	protected MyDBHelper db;
	protected Bitmap _bmp;
	protected boolean isEdit = false;
	private int pid = -1;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setBackgroundDrawableResource(R.drawable.bg);
		setTitle(R.string.desc_add_picture);
		setContentView(R.layout.panel_add_picture);

		Bundle bundle = this.getIntent().getExtras();
		isEdit = bundle.getBoolean("edit");
		pid = bundle.getInt("id");
		String name = bundle.getString("name");
		String detail = bundle.getString("detail");

		_name = (EditText) findViewById(R.id.name);
		_detail = (EditText) findViewById(R.id.detail);
		_image = (ImageView) findViewById(R.id.image);

		if (isEdit == true) {
			_name.setText(name);
			_detail.setText(detail);
		}

		// Add Button Listener
		Button b = (Button) findViewById(R.id.BtnGoBack);
		b.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				fnBackToActivity();
			}
		});
		// Add Button Listener
		Button ba = (Button) findViewById(R.id.BtnTakePicture);
		ba.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				startCameraActivity();
			}
		});
		// Add Button Listener
		Button bs = (Button) findViewById(R.id.BtnSave);
		bs.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				fnSaveDataToDB();
			}
		});
	}

	protected void fnSaveDataToDB() {
		Log.i("PanelAddPicture", "fnSaveDataToDB");
		if (_name.getText().toString().equalsIgnoreCase("")) {
			Toast.makeText(PanelAddPlayer.this,
					"Please in the name of the picture.", Toast.LENGTH_SHORT)
					.show();
			return;
		} else {
			Picture p = new Picture();
			if (isEdit == true) {
				p.setId(pid);
			}
			p.setName(_name.getText().toString());
			p.setDetail(_detail.getText().toString());
			p.setImage(_bmp);
			if (db == null) {
				db = new MyDBHelper(this);
			}

			db.open();
			long result = -1;
			if (isEdit == true) {
				result = db.updatePicutre(p);
			} else {
				result = db.addPicture(p);
			}

			db.close();
			if (result == -1) {
				Toast.makeText(PanelAddPlayer.this,
						"Fail to insert picture to database.",
						Toast.LENGTH_SHORT).show();
				return;
			} else {
				Toast.makeText(PanelAddPlayer.this,
						"Insert picture to database.", Toast.LENGTH_SHORT)
						.show();
				fnBackToActivity();
			}
		}
	}

	protected void fnBackToActivity() {
		Intent myIntent = new Intent(PanelAddPlayer.this, PanelEditPlayer.class);
		PanelAddPlayer.this.startActivity(myIntent);
	}

	protected void startCameraActivity() {
		Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
		startActivityForResult(i, 0);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (resultCode) {
		case 0:
			break;

		case -1:
			Bitmap bmp = (Bitmap) data.getExtras().get("data");
			onPhotoTaken(bmp);
			break;
		}
	}

	protected void onPhotoTaken(Bitmap a) {
		_taken = true;
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inSampleSize = 4;
		options.inScaled = true;
		_bmp = a;
		_image.setImageBitmap(a);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		Log.i("MakeMachine", "onRestoreInstanceState()");
		if (savedInstanceState.getBoolean(PHOTO_TAKEN)) {
			onPhotoTaken(null);
		}
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		outState.putBoolean(PHOTO_TAKEN, _taken);
	}
}